package com.itheima01;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class Test03 {
    public static void main(String[] args) throws IOException {
        MulticastSocket s = new MulticastSocket(5555);
        s.joinGroup(InetAddress.getByName("224.0.0.2"));
        while (true) {
            byte[] bytes = new byte[1024];
            DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
            s.receive(packet);
            InetAddress address = packet.getAddress();
            int length = packet.getLength();
            String s1 = new String(bytes, 0, length);
            System.out.println(s1);
            System.out.println(address.getHostName() + "在说+:" + s1);
        }
    }
}
